#ifndef IO_HANDLE_H_
#define IO_HANDLE_H_

#ifdef __cplusplus
extern "C"
{
#endif

#include "mytype.h"
#include "dataproc.h"
#include "key_handle.h"
#include "io_flt_sig.h"
#include "io_flt_group.h"

	uint8_t ad_io_creat(uint8_t sample_old, uint16_t ad_true, uint16_t ad_false, uint16_t ad_now);

	struct ad_filter_str
	{
		uint8_t sum_list_index; 
		uint8_t sum_list_len;	
		uint32_t *sum_list;		
		uint32_t *sum_now;		
		uint8_t sample_cnt;		
		uint8_t sample_set;		
		uint8_t sum_shift_bits; 
		uint8_t ch_amount;		
		uint8_t b_end_st;		
	};
	typedef struct ad_filter_str ad_filter_type;
	void ad_oversampling(ad_filter_type *obj, uint16_t ad_now[]);

	struct out_obj_struct
	{
		void (*en)(void);
		void (*dis)(void);
		void (*tog)(void);
		uint8_t (*out_st)(void);
	};

	typedef struct out_obj_struct out_obj_type;

	struct io_signal_struct
	{
		uint8_t sig;
		uint8_t edge, edge_found, edge_lost;
		uint16_t found_time_cnt;
		uint16_t lost_time_cnt;
	};

	typedef struct io_signal_struct io_signal_type;

	uint8_t io_signal_info_handle(io_signal_type *io_obj, uint8_t time_pulse, uint8_t sig_new);

	typedef struct
	{
		word_tt old;
		word_tt edge, edge_found, edge_lost;
	} u16_group_edge_type;

	typedef struct
	{
		dword_tt old;
		dword_tt edge, edge_found, edge_lost;
	} u32_group_edge_type;

	uint16_t u16_group_edge_gen(u16_group_edge_type *obj, uint16_t rt_val);
	uint32_t u32_group_edge_gen(u32_group_edge_type *obj, uint32_t rt_val);

	struct out_sw_struct
	{
		uint8_t out;		 
		uint8_t en_edge;	 
		uint8_t dis_edge;	 
		state_req_type req; 
		uint32_t time;		 
	};

	typedef struct out_sw_struct out_sw_type;

	uint8_t out_sw_manage(out_sw_type *out_obj, uint32_t en_time_sv, uint32_t dis_time_sv, uint8_t pulse);

	void out_sw_reset(out_sw_type *out_obj, uint8_t out_st, state_req_type start_req);

	struct wt_add_struct
	{
		uint8_t run_st;	   
		uint8_t low_st;	   
		uint8_t high_st;   
		uint8_t out;	   
		uint8_t err;	   
		uint8_t comp;	   
		uint16_t en_time;  
		uint16_t dly_time; 
	};

	typedef struct wt_add_struct wt_add_type;

	uint8_t wt_add_manage(wt_add_type *out_obj, uint16_t ot_time_sv, uint16_t dly_time_sv, uint8_t pulse);

	struct dev_double_sw_struct
	{
		uint8_t out0;			  
		uint8_t out1;			  
		uint8_t mode;			  
		uint8_t out_sel;		  
		int32_t out0_en_time;	  
		int32_t out0_dis_time;	  
		int32_t out1_en_time;	  
		int32_t out1_dis_time;	  
		int32_t delay_cnt;		  
		int32_t dev_off_min_time; 
	};

	typedef struct dev_double_sw_struct dev_double_sw_type;

	void dev_double_sw(dev_double_sw_type *dev, int32_t switch_time_set, int32_t switch_time_befor, uint8_t pulse);

	typedef enum
	{
		C_ST_NC_STOP = 0,
		C_ST_CC_RUN = 1,
		C_ST_CC_STOP = 2,
		C_ST_NC_RUN = 3,
	} nc_cc_sw_st_type;

	struct nc_cc_sw_struct
	{
		nc_cc_sw_st_type st;	   
		nc_cc_sw_st_type st_start; 
		uint32_t cct_set;		   
		uint32_t nct_set;		   
		uint32_t sot_set;		   
		uint32_t spd_now;		   
		uint32_t spd_set;		   
		uint32_t time_cnt;		   
		uint16_t cycle;			   
		uint8_t steady;			   
		uint8_t time_mode;		   
	};

	typedef struct nc_cc_sw_struct nc_cc_sw_type;

	nc_cc_sw_st_type nc_cc_sw_handle(nc_cc_sw_type *obj, uint8_t en, uint8_t pulse);

	struct spd_inc_struct
	{
		uint32_t spd_now;		   
		uint32_t spd_set;		   
		uint32_t spd_out;		   
		uint32_t spd_up;		   
		uint32_t spd_dn;		   
		uint32_t spd_min;		   
		uint8_t diff_up_mul;	   
		uint8_t diff_dn_mul;	   
		uint8_t pwm_out_under_min; 
	};

	typedef struct spd_inc_struct spd_inc_type;

	uint32_t spd_inc_handle(spd_inc_type *obj, uint8_t pulse);

	struct beep_notice_struct
	{
		union
		{
			uint32_t st; 
			struct
			{
				uint8_t micro_x;  
				uint8_t short_x;  
				uint8_t middle_x; 
				uint8_t auto_x;	  
			} mode;
		} req;

		uint8_t auto_period; 
		uint8_t auto_duty;	 
		uint8_t pulse_cnt;	 
		uint8_t b_beep_st;	 
	};

	typedef struct beep_notice_struct beep_notice_type;

	void beep_handle(beep_notice_type *obj, uint8_t bpulse);
	void beep_clear(beep_notice_type *obj);
	void beep_auto_req(beep_notice_type *obj, uint8_t req_cnt, uint8_t period, uint8_t duty);
	void beep_micro_req(beep_notice_type *obj, uint8_t req_cnt);
	void beep_short_req(beep_notice_type *obj, uint8_t req_cnt);
	void beep_middle_req(beep_notice_type *obj, uint8_t req_cnt);

	struct comp_out_struct
	{
		uint8_t run_st;	  
		uint8_t out;	  
		uint8_t out_edge; 
		int8_t dir;		  
		lag16_type lag;	  
		int16_t lag_rang; 

		int16_t sv;			
		int16_t pv;			
		int16_t en_offset;	
		int16_t dis_offset; 
		int16_t vd_dir;		
	};

	typedef struct comp_out_struct comp_out_type;

	uint8_t comp_out_handle(comp_out_type *obj, uint8_t dir_adj_pulse);		
	uint8_t comp_out_nev_handle(comp_out_type *obj, uint8_t dir_adj_pulse); 

	typedef struct
	{
		uu08 st;		 
		uu08 st_old;	 
		uu08 out1, out2; 
		uu16 tick_start; 
	} sequence_linkage_type;

	void sequence_linkage_manage(sequence_linkage_type *obj, uu16 delay, uu16 ticks);

	typedef struct
	{
		uu08 st;	   
		uu08 st_old;   
		uu08 obits;	   
		u16_tt out;	   
		uu16 tick_cnt; 
		uu16 tick_old; 
	} sequence16_linkage_type;

	void sequence16_linkage_manage(sequence16_linkage_type *obj, uu08 list_len, uu16 delay, uu16 ticks);

	typedef struct
	{
		uu08 st;	   
		uu08 st_old;   
		uu08 obits;	   
		u08_tt out;	   
		uu16 tick_cnt; 
		uu16 tick_old; 
	} sequence08_linkage_type;

	void sequence08_linkage_manage(sequence08_linkage_type *obj, uu08 list_len, uu16 delay, uu16 ticks);

#define OUT_EXE_STEP_IDLE 0
#define OUT_EXE_STEP_OUT 1
#define OUT_EXE_STEP_DLY 2
#define OUT_EXE_STEP_SPACE 3

	struct out_exe_struct
	{
		uint8_t req;			
		uint8_t condition;		
		uint8_t st_back;		
		uint8_t exe_max;		
		uint16_t exe_time_ot;	
		uint16_t exe_time_dly;	
		uint16_t space_time_sv; 

		uint16_t tick_start; 
		uint8_t exe_step;	 
		uint8_t exe_cnt;	 
		uint8_t err;		 
		uint8_t out;		 
	};

	typedef struct out_exe_struct out_exe_type;

	void out_exe_handle(out_exe_type *obj);

	typedef enum
	{
		OPCL_IDLE = 0,

		OPCL_OP_START = 10,
		OPCL_OP_EXE,
		OPCL_OP_OK_DELAY,
		OPCL_OP_OUT_DELAY,

		OPCL_CL_START = 20,
		OPCL_CL_EXE,
		OPCL_CL_OK_DELAY,
		OPCL_CL_OUT_DELAY,
	} op_cl_step_type;

	typedef struct
	{
		op_cl_step_type step;
		op_cl_step_type step_old;
		lib_rs_cmd_req_type req; 

		uu08 open_st : 1;		
		uu08 close_st : 1;		
		uu08 open_out : 1;		
		uu08 close_out : 1;		
		uu08 open_err : 1;		
		uu08 close_err : 1;		
		uu08 suspend : 1;		
		uu08 out_merge_old : 1; 
		uu08 exe_cnt;			
		uu08 exe_max;			
		uu08 exe_min;			
		uu16 tick_cnt;			
		uu16 out_none_tick_cnt; 
		uu16 suspend_tick_cnt;	
		uu16 tick_old;			
		uu16 timeout;			
		uu16 ok_delay;			
		uu16 out_delay;			
	} op_cl_exe_type;

	void op_cl_exe_handle(op_cl_exe_type *obj);

#ifdef __cplusplus
}
#endif

#endif 
